package com.avcdata.aoscustommade.utils.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.avcdata.aoscustommade.bean.BaseResponseBean;
import com.avcdata.aoscustommade.utils.AvcLogUtils;

import java.util.List;
import java.util.Map;

/**
 * Json工具类
 *
 * Created by zyh on 2017/5/22.
 */

public class AvcJsonUtils {

    public AvcJsonUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static String object2Json(Object bean){
        String result = null;
        if (bean != null){
            try {
                result = JSON.toJSONString(bean, false);
            } catch (JSONException e){
                e.printStackTrace();
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            } catch (Throwable e){
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            }
        }

        return result;
    }


    public static <T> T json2object(String jsonString, Class<T> clazz){
        T result = null;
        if (null != jsonString){
            try {
                result = JSON.parseObject(jsonString, clazz);
            } catch (JSONException e){
                e.printStackTrace();
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            } catch (Throwable e){
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            }
        }

        return result;
    }

    public static <T> List<T> json2list(String jsonString, Class<T> t){

        List<T> result = null;
        if (null != jsonString){
            try {
                result = JSON.parseArray(jsonString, t);
            } catch (JSONException e){
                e.printStackTrace();
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            } catch (Throwable e){
                AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
            }
        }

        return result;
    }

    public static <T> T json2object(String jsonString, TypeReference<T> typeReference){

        T result = null;
        if (null != jsonString){
          try {
              result = JSON.parseObject(jsonString, typeReference);
          } catch (JSONException e){
              e.printStackTrace();
              AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
          } catch (Throwable e){
              AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
          }
        }
        return result;
    }

    public static Map<String, Object> jsonObjectToMap(org.json.JSONObject jsonObject){
        Object result = JSON.parseObject(jsonObject.toString(), Map.class);
        return (Map<String, Object>) result;
    }

    public static List<Map> jsonObjectToMList(JSONObject jsonobject){
        Object result = JSON.parseArray(jsonobject.toString(), Map.class);
        return (List<Map>) result;
    }

    public static Object jsonObjectToObject(org.json.JSONObject jsonObject, Class classObject){
        try {
            return JSON.parseObject(jsonObject.toString(), classObject);
        } catch (JSONException e){
            AvcLogUtils.d("AvcJsonUtils", "json转换异常" + e.getMessage());
        }

        BaseResponseBean bean = new BaseResponseBean();
        bean.setSuccess(false);
        bean.setMessage("网络错误");
        return bean;
    }
}
